<?
 class page extends DM{
//
//
// Save the pages
// 
// 
// VERSION: 
// AUTHOR: bugz
// DATE: 30.12.2006
// 


// VARIABLE DECLARATION
	var $id_page = "";
	var $id_site = "";
	var $txt_url = "";
	var $txt_query = "";
	var $title = "";

	var $debugging=false;

	var $lista = "";


// METHOD DECLARATION

//DEBUG METHOD
	function debug($result,$operation=""){
		if ($result){
			if ($this->debugging){
				echo "<div style='font-size:10px;font-family:arial,sans;color:#FFFFFF;background-color:#008800'>OK: ";
				echo strtoupper($this->db_type)."($operation) - [No errors computed] ";
				echo "</div>";
			}
			return true;
		}else{
			if ($this->debugging){
				echo "<div style='font-family:arial,sans;color:#FFFFFF;background-color:#ff0000;font-size:10px;'>ERROR: ";
				echo strtoupper($this->db_type)."($operation) - [".$this->db->errDesc."] ".$this->db->errDesc;
				echo "</div>";
			}
			return false;
		}
		
	}

//CONSTRUCTOR METHOD
	function page(){
		//LOAD THE DATABSE CLASS
		$this->loadClass('database','database');

		//INITIALIZE THE VARIABLES
		$this->id_page = "";
		$this->id_site = "";
		$this->txt_url = "";
		$this->txt_query = "";
		$this->title = "";
		$this->lista = array();

	}

//GET METHODS
//
//
//
	function getList(){

		//SQL STRING
		$sql="SELECT id_page FROM tbl_pages WHERE 1";
		$db= new database();
		$db->debugging=$this->debugging;
		if ($db->connect()){
			if ($db->fetch($sql)){
				$this->lista=$db->resultArray;
				return $this->debug(true,'get_list');
			}else{
				$this->errorReport("Error in the select",$sql);
				return $this->debug(false,"get_List - $sql");
			}
		}else{
			$this->errorReport("Could not conncet to the database",$db);
			return $this->debug(false,'get_list');
		}
	}

	function getById(){
		if ($this->id_page!=""){
			$sql = "select * from tbl_pages where id_page='$this->id_page'";
			$db= new database();
			$db->debugging=$this->debugging;
			if ($db->connect()){
				if ($db->fetch($sql)){
					if (count($db->resultArray)==1){
						$this->id_page = $db->resultArray[0]["id_page"];
						$this->id_site = $db->resultArray[0]["id_site"];
						$this->txt_url = $db->resultArray[0]["txt_url"];
						$this->txt_query = $db->resultArray[0]["txt_query"];
						$this->title = $db->resultArray[0]["title"];
						return true;
					}else{
						$this->errorReport("Error too many results.",$sql);
						return false;
					}
				}else{
					$this->errorReport("Error in the select",$sql);
					return false;
				}
			}else{
				$this->errorReport("Could not conncet to the database",$db);
				return false;
			}
		}
	}
	function checkSave(){
		$sql = "select id_page from tbl_pages where txt_url='".$this->txt_url."' and txt_query='".$this->txt_query."'";
		$db= new database();
		$db->debugging=$this->debugging;
		if ($db->connect()){
			if ($db->fetch($sql)){
				if (count($db->resultArray)==1){
					$this->id_page = $db->resultArray[0]["id_page"];
					return $this->save();
				}else{
					return $this->save();
				}
			}else{
				$this->errorReport("Error in the select",$sql);
				return false;
			}
		}else{
			$this->errorReport("Could not conncet to the database",$db);
			return false;
		}
	}
	function save(){
		$u="";
		if ($this->id_page==""){
			//insert
			$u=false;
			$sql = "";
			$sql .= "INSERT INTO tbl_pages(";
			$sql .= " id_page";
			$sql .= ", id_site";
			$sql .= ", txt_url";
			$sql .= ", txt_query";
			$sql .= ", title";
			$sql .= ") VALUES (";
			$sql .= " ''";
			$sql .= ", '$this->id_site'";
			$sql .= ", '$this->txt_url'";
			$sql .= ", '$this->txt_query'";
			$sql .= ", '$this->title'";
			$sql .= ")";
		}else{
			//update
			$u=true;
			$sql = "";
			$sql .= "UPDATE tbl_pages SET";
			$sql .= " id_site";
			$sql .= " = '$this->id_site'";
			$sql .= ", txt_url";
			$sql .= " = '$this->txt_url'";
			$sql .= ", txt_query";
			$sql .= " = '$this->txt_query'";
			$sql .= ", title";
			$sql .= " = '$this->title'";
			$sql .= " where ";
			$sql .= " id_page = '".$this->id_page."'";
		}
		$db= new database();
		$db->debugging=$this->debugging;
		if ($db->connect()){
			if ($db->execute($sql)){
				if (!$u){
					$this->id_page=$db->lastInsertID();
				}
				return true;
			}else{
				$this->errorReport("Error in the select",$sql);
				return false;
			}
		}else{
			$this->errorReport("Could not conncet to the database",$db);
			return false;
		}
	}
	function delete(){
		$sql="DELETE FROM tbl_pages WHERE id_page='$this->id_page'";
		$db= new database();
		$db->debugging=$this->debugging;
		if ($db->connect()){
			if ($db->execute($sql)){
				return true;
			}else{
				$this->errorReport("Error in the SQL:<BR>",$sql);
				return false;
			}
		}else{
			$this->errorReport("Error in the Connect");
			return false;
		}
	}


}
?>
